package turtle.system.pt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * 可用於細步研究某一特定時間區間
 * @author doug
 *
 */
public class PeriodEvaluator {
	public static String initialDate = "2009/01/01";
	public static String endDate = "2009/02/25";
	public static GregorianCalendar startCal, endCal;
	static Date leastDay = null;
	public static final String WTX0_YEARS_FN = "tmp/1000-partial.txt";
	public static final String WTX1_YEARS_FN = "tmp/WTX1-partial.txt";
	private Date startTime, endTime;

	public void beginProc(Date startTime, Date endTime) {
		this.startTime = startTime;
		this.endTime = endTime;

		getPartialData();

		try {
			exec();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private boolean isBetween(Date day) {
		return (!day.before(startTime) && !day.after(endTime));
	}

	private void getPartialData() {
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		try {
			BufferedReader reader = null;
			BufferedWriter writer = null;
			String line;
			reader = new BufferedReader(new FileReader(DailyExec.WTX1_PREFIX_FN
					+ DailyExec.FN_SUFFIX));
			reader.readLine();
			reader.readLine();
			reader.readLine();
			writer = new BufferedWriter(new FileWriter(WTX1_YEARS_FN));
			writer.write(DailyExec.WTX1_WORD);
			while ((line = reader.readLine()) != null) {
				String d = line.split(" 	")[0];
				if (isBetween(df.parse(d))) {
					writer.write(line + "\n");
				}
			}
			reader.close();
			writer.flush();
			writer.close();

			reader = new BufferedReader(new FileReader(DailyExec.WTX0_PREFIX_FN
					+ DailyExec.FN_SUFFIX));
			reader.readLine();
			reader.readLine();
			reader.readLine();
			writer = new BufferedWriter(new FileWriter(WTX0_YEARS_FN));
			writer.write(DailyExec.WTX0_WORD);
			while ((line = reader.readLine()) != null) {
				String d = line.split(" 	")[0];
				if (isBetween(df.parse(d))) {
					writer.write(line + "\n");
				}
			}
			reader.close();
			writer.flush();
			writer.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public PeriodEvaluator() {

		startCal = new GregorianCalendar();
		endCal = new GregorianCalendar();

		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date s = null;
		try {
			s = df.parse(initialDate);
			leastDay = df.parse(endDate);

			startCal.setTime(s);
			endCal.setTime(leastDay);
		} catch (Exception e) {
			;
		}
		this.beginProc(startCal.getTime(), endCal.getTime());

	}

	public void exec() throws IOException {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		PriceData cons = new PriceData(PriceData.STRATEGY_CONSERVATIVE,
				WTX0_YEARS_FN, WTX1_YEARS_FN);

		PriceData posi = new PriceData(PriceData.STRATEGY_POSITIVE,
				WTX0_YEARS_FN, WTX1_YEARS_FN);
	}
	public static void main(String[] args) {
		new PeriodEvaluator();
	}
}
